package twentyone;

import java.io.*;
import java.util.*;

public class 今天你健康打卡了吗 {
    static int[] weights;
    static int[][] dp;
    static List<List<Integer>> tree;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine().trim());

        weights = new int[n + 1];
        dp = new int[n + 1][2];
        tree = new ArrayList<>(n + 1);

        for (int i = 0; i <= n; i++) {
            tree.add(new ArrayList<>());
        }

        for (int i = 1; i <= n; i++) {
            weights[i] = Integer.parseInt(br.readLine().trim());
        }

        for (int i = 1; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());
            tree.get(x).add(y);
        }

        dfs(1);
        int result = Math.max(dp[1][0], dp[1][1]);
        System.out.println(result);
    }

    static void dfs(int u) {
        dp[u][0] = 0;
        dp[u][1] = weights[u];

        for (int v : tree.get(u)) {
            dfs(v);
            dp[u][0] += Math.max(dp[v][0], dp[v][1]);
            dp[u][1] += dp[v][0];
        }
    }
}